<template>
  <el-dialog
    :title="!dataForm.bid ? '新增' : '处理报修单'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label-width="100px" label="报修单车编号" prop="bid">
      <el-input v-model="dataForm.bid" placeholder="报修单车编号"></el-input>
    </el-form-item>
    <el-form-item label-width="100px" label="电单车状态" prop="bstate">
      <el-input v-model="dataForm.bstate" placeholder="未借 已借 需维修 报废 已维修"></el-input>
    </el-form-item>
    <el-form-item label-width="100px" label="更新时间" prop="bcreateTime">
      <el-input v-model="dataForm.bupdateTime" placeholder="更新时间(格式:2020-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label-width="100px" label="备注" prop="bcomment">
      <el-input v-model="dataForm.bcomment" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bid: 0,
          bname: '',
          bicon: '',
          bcid: '',
          binTime: '',
          bstate: '',
          bcreateTime: '',
          bupdateTime: '',
          bcomment: ''
        },
        dataRule: {
          bname: [
            { required: true, message: '单车名字不能为空', trigger: 'blur' }
          ],
          bicon: [
            { required: true, message: '单车图片不能为空', trigger: 'blur' }
          ],
          bcid: [
            { required: true, message: '单车属于的分类不能为空', trigger: 'blur' }
          ],
          binTime: [
            { required: true, message: '入库时间不能为空', trigger: 'blur' }
          ],
          bstate: [
            { required: true, message: '未借  已借 需维修 报废不能为空', trigger: 'blur' }
          ],
          bcreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          bupdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          bcomment: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/bike/info/${this.dataForm.bid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bname = data.bike.bname
                this.dataForm.bicon = data.bike.bicon
                this.dataForm.bcid = data.bike.bcid
                this.dataForm.binTime = data.bike.binTime
                this.dataForm.bstate = data.bike.bstate
                this.dataForm.bcreateTime = data.bike.bcreateTime
                this.dataForm.bupdateTime = data.bike.bupdateTime
                this.dataForm.bcomment = data.bike.bcomment
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/bike/${!this.dataForm.bid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bid': this.dataForm.bid || undefined,
                'bname': this.dataForm.bname,
                'bicon': this.dataForm.bicon,
                'bcid': this.dataForm.bcid,
                'binTime': this.dataForm.binTime,
                'bstate': this.dataForm.bstate,
                'bcreateTime': this.dataForm.bcreateTime,
                'bupdateTime': this.dataForm.bupdateTime,
                'bcomment': this.dataForm.bcomment
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
